<?php
namespace app\manage\controller;
use think\Db;

class RebateOrder extends Base
{
    public function index(){
        if(request()->isAjax()){
            $page = input('page');
            $limit = input('limit');
            $myUserId = input('my_user_id');
            $roleId = input('role_id');
            $serverId = input('server_id');
            $myOrderId = input('my_order_id');
            $gameId = input('game_id');
            $channelId = input('channel_id');

            $where = 1;
            !empty($myUserId) && $where.= " && (userid='{$myUserId}' OR loginname like '%{$myUserId}%')";
            !empty($roleId) && $where.= " && (roleid='{$roleId}' OR rolename like '%{$roleId}%')";
            !empty($serverId) && $where.= " && (serverid='{$serverId}' OR servername like '%{$serverId}%')";
            !empty($myOrderId) && $where.= " && orderid='{$myOrderId}'";
            !empty($gameId) && $where.= " && gameid='{$gameId}'";
            if(!empty($channelId)){
                if($channelId<=5000){ //CPS查最右边4位
                    $where.= " && right(channelid, 4)='{$channelId}'";
                }else{ //联运查最左边4位
                    $where.= " && left(channelid, 4)='{$channelId}'";
                }
            }

            if( !$count = model('RebatedOrder')->where($where)->count() ){
                $this->layTable();
            }

            $select = model('RebatedOrder')
                ->where($where)
                ->order('applytime desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();

            $channelIdName = model('Channel')->channelIdName();
            $gameIdName = model('Game')->gameIdName();
            $data = [];
            foreach($select as $k=>$v){
                $cId = substr($v['channelid'], 0, 4);
                if($cId==1000){
                    $cId = substr($v['channelid'], 5);
                }
                $data[] = array(
                    'apply_id' => $v['applyid'],
                    'rule' => $v['rule'],
                    'rebate_game_coin' => $v['rebate_gamecoin'],
                    'apply_time' => date('m-d H:i:s', $v['applytime']),
                    'my_order_id' => $v['orderid'],
                    'channel_id' => $cId . '：' . $channelIdName[ $cId ],
                    'game_id' => $v['gameid'] . '：' . $gameIdName[ $v['gameid'] ],
                    'server_id' => $v['serverid'] . '：' . $v['servername'],
                    'my_user_id' => $v['userid'] . '：' . $v['loginname'],
                    'role_id' => $v['roleid'] . '：' . $v['rolename'],
                    'total_price' => $v['amount'],
                    'game_coin' => $v['gamecoin'],
                    'pay_time' => date('m-d H:i:s', $v['paytime']),
                );
            }

            $this->layTable(0, '已返利订单', $data, $count);
        }else{
            return view();
        }
    }
}